package cn.iocoder.yudao.module.strategy.controller.admin.strategy;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.strategy.controller.admin.strategy.vo.*;
import cn.iocoder.yudao.module.strategy.dal.dataobject.strategy.StrategyDO;
import cn.iocoder.yudao.module.strategy.convert.strategy.StrategyConvert;
import cn.iocoder.yudao.module.strategy.service.strategy.StrategyService;

@Tag(name = "管理后台 - 企业战略（短期、长期）")
@RestController
@RequestMapping("/strategy/")
@Validated
public class StrategyController {

    @Resource
    private StrategyService strategyService;

    @PostMapping("/create")
    @Operation(summary = "创建企业战略（短期、长期）")
    @PreAuthorize("@ss.hasPermission('strategy:create')")
    public CommonResult<Long> create(@Valid @RequestBody StrategyCreateReqVO createReqVO) {
        return success(strategyService.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新企业战略（短期、长期）")
    @PreAuthorize("@ss.hasPermission('strategy:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody StrategyUpdateReqVO updateReqVO) {
        strategyService.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除企业战略（短期、长期）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('strategy:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        strategyService.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得企业战略（短期、长期）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('strategy:query')")
    public CommonResult<StrategyRespVO> get(@RequestParam("id") Long id) {
        StrategyRespVO strategyRespVO = strategyService.getStrategyRespVO(id);
        return success(strategyRespVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得企业战略（短期、长期）列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('strategy:query')")
    public CommonResult<List<StrategyRespVO>> getList(@RequestParam("ids") Collection<Long> ids) {
        List<StrategyDO> list = strategyService.getList(ids);
        return success(StrategyConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得企业战略（短期、长期）分页")
    @PreAuthorize("@ss.hasPermission('strategy:query')")
    public CommonResult<PageResult<StrategyRespVO>> getPage(@Valid StrategyPageReqVO pageVO) {
        PageResult<StrategyRespVO> strategyRespVOPageResult = strategyService.getPageStrategyRespVO(pageVO);
        return success(strategyRespVOPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出企业战略（短期、长期） Excel")
    @PreAuthorize("@ss.hasPermission('strategy:export')")
    @OperateLog(type = EXPORT)
    public void exportExcel(@Valid StrategyExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<StrategyDO> list = strategyService.getList(exportReqVO);
        // 导出 Excel
        List<StrategyExcelVO> datas = StrategyConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "企业战略（短期、长期）.xls", "数据", StrategyExcelVO.class, datas);
    }

}
